package com.cityfood.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cityfood.mapper.CfAdminUserMapper;
import com.cityfood.pojo.CfAdminUser;
import com.cityfood.pojo.CfAdminUserExample;
import com.cityfood.pojo.CfAdminUserExample.Criteria;
import com.cityfood.service.AdminUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class AdminUserServiceImpl implements AdminUserService {

	@Autowired
	private CfAdminUserMapper adminUserMappermapper;
	
	@Override
	public CfAdminUser getCfAdminUserByNameANDPass(String name, String pass) {
		CfAdminUserExample example = new CfAdminUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andUsernameEqualTo(name);
		criteria.andPasswordEqualTo(pass);
		List<CfAdminUser> adminUsers = adminUserMappermapper.selectByExample(example);
		CfAdminUser adminUser = adminUsers.get(0);
		return adminUser;
	}

	@Override
	public CfAdminUser getAdminUserById(Integer id) {
		CfAdminUser adminUser = adminUserMappermapper.selectByPrimaryKey(id);
		return adminUser;
	}
	
	@Override
	public CfAdminUser getAdminUserByPhone(String phone){
		CfAdminUserExample example = new CfAdminUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andPhoneEqualTo(phone);
		List<CfAdminUser> adminUsers = adminUserMappermapper.selectByExample(example);
		if(adminUsers != null && adminUsers.size() > 0){
			CfAdminUser adminUser = adminUsers.get(0);
			return adminUser;
		}else{
			return null;
		}
	}

	@Override
	public int saveAdminUser(CfAdminUser adminUser) {
		int i = adminUserMappermapper.insertSelective(adminUser);
		return i;
	}

	@Override
	public int updateAdminUserById(CfAdminUser adminUser) {
		int i = adminUserMappermapper.updateByPrimaryKeySelective(adminUser);
		return i;
	}

	@Override
	public int deleteAdminUserById(Integer id) {
		int i = adminUserMappermapper.deleteByPrimaryKey(id);
		return i;
	}
	
	@Override
	public CfAdminUser getAdminUserByName(CfAdminUser adminUser) {
		if (adminUser.getUsername() !=null && adminUser.getUsername() !="") {
			CfAdminUserExample example = new CfAdminUserExample();
			Criteria criteria = example.createCriteria();
			criteria.andUsernameEqualTo(adminUser.getUsername());
			List<CfAdminUser> list = adminUserMappermapper.selectByExample(example);
			if (list.size()!=0) {
				CfAdminUser user = list.get(0);
			if (!user.getId().equals(adminUser.getId())) {
				return user;
			}
			}
		}
		return null;
	}

	@Override
	public CfAdminUser getAdminUserByPhone(CfAdminUser adminUser) {
		if (adminUser.getPhone() !=null && adminUser.getPhone() !="") {
			CfAdminUserExample example = new CfAdminUserExample();
			Criteria criteria = example.createCriteria();
			criteria.andPhoneEqualTo(adminUser.getPhone());
			List<CfAdminUser> list = adminUserMappermapper.selectByExample(example);
			if (list.size()!=0) {
				CfAdminUser user = list.get(0);
			if (!user.getId().equals(adminUser.getId())) {
				return user;
			}
			}
		}
		return null;
	}

	@Override
	public CfAdminUser getAdminUserByEmail(CfAdminUser adminUser) {
		if (adminUser.getEmail() !=null && adminUser.getEmail() !="") {
			CfAdminUserExample example = new CfAdminUserExample();
			Criteria criteria = example.createCriteria();
			criteria.andEmailEqualTo(adminUser.getEmail());
			List<CfAdminUser> list = adminUserMappermapper.selectByExample(example);
			if (list.size()!=0) {
				CfAdminUser user = list.get(0);
			if (!user.getId().equals(adminUser.getId())) {
				return user;
			}
			}
		}
		return null;
	}

	@Override
	public PageInfo<CfAdminUser> getAdminUserByCondition(String name, String phone, Integer role,Integer pageNum) {
		//设置分页的页数和行数
		PageHelper.startPage(pageNum, 20);
		
		CfAdminUserExample example = new CfAdminUserExample();
		Criteria criteria = example.createCriteria();
		if (!name.equals("")) {
			criteria.andUsernameLike("%"+name+"%");
		}
		if (!phone.equals("")) {
			criteria.andPhoneLike(phone+"%");
		}
		if (role!=100) {
			criteria.andRoleEqualTo(role.byteValue());
		}
		List<CfAdminUser> adminUsers = adminUserMappermapper.selectByExample(example);
		
		PageInfo<CfAdminUser> info = new PageInfo<>(adminUsers);
		return info;
	}

}
